package com.zxl.manager.dao.impl;

import com.zxl.manager.dao.UserDao;
import com.zxl.manager.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by jimmy on 2017/8/12.
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public User getUserByName(String name) {
        User user = new User();
        String sql = "select * from system_user where username = ?";
        List<User> list = jdbcTemplate.query(sql, new Object[]{name}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user1 = new User();
                user1.setUserId(resultSet.getLong("suser_id"));
                user1.setName(resultSet.getString("username"));
                user1.setPassword(resultSet.getString("password"));
                user1.setSalt(resultSet.getString("salt"));
                user1.setStatus(resultSet.getInt("status"));
                user1.setCreateTime(resultSet.getTimestamp("create_time"));
                user1.setLoginDate(resultSet.getTimestamp("last_login_time"));
                return user1;
            }
        });
        if (list.size() > 0){
            user = list.get(0);
        }
        return user;
    }
}
